package com.catfacts;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;


public class Datastore {
	
	private static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	
	public static final String SUBSCRIPTION_KIND = "SUBSCRIPTION";
	
	
	public static void addSubscriber(String email, String number){
		if(email != null) {
			Transaction txn = datastore.beginTransaction();
			Entity e = new Entity(SUBSCRIPTION_KIND);
			e.setProperty("email", email);
			e.setProperty("number", number);
			datastore.put(e);
			txn.commit();
		}
	}
	
	public static List<Entity> querySubscribers(){
		Query q = new Query(SUBSCRIPTION_KIND);
		PreparedQuery pq = datastore.prepare(q);
		Iterator<Entity> i = pq.asIterator();
		List<Entity> list = new ArrayList<Entity>();
		while(i.hasNext()) {
			Entity e = i.next();
			list.add(e);
		}
		return list;
	}

}
